//
//  HELCourseListByCatogryVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/2/20.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELCourseListByCatogryVC.h"
#import "HELCourseCell.h"
#import "Masonry.h"
#import "HELChooseCategoryVC.h"
#import "NHCourseDetailVC.h"
@interface HELCourseListByCatogryVC ()


@property (nonatomic,  strong) UIView *topView;


@end

@implementation HELCourseListByCatogryVC



- (UIView *)topView{
    if (!_topView) {
        _topView = [UIView new];
        _topView.frame = CGRectMake(0, 0, kScreenWidth, 49);
        _topView.backgroundColor =[UIColor whiteColor];
        
    
            
            _topView = [UIView new];
            _topView.backgroundColor =kAppBg;
            _topView.layer.borderWidth = 1;
            _topView.layer.borderColor = kBorderColor.CGColor;
            UILabel *label =[UILabel new];
        
            label.text = @"当前分类课程";
            
            
            
            label.font = [UIFont systemFontOfSize:17];
            [_topView addSubview:label];
            
            
            UIImageView *imageView =[UIImageView new];
            imageView.image =[UIImage imageNamed:@"cell_right"];
            [_topView addSubview:imageView];
            
            
            UILabel *rightL =[UILabel new];
            rightL.text = @"全部";
            if (self.catogryName) {
                rightL.text = self.catogryName;
            }
            rightL.tag = 201;
            rightL.textColor = kTxtColor;
            rightL.textAlignment = NSTextAlignmentRight;
            rightL.font = [UIFont systemFontOfSize:15];
            [_topView addSubview:rightL];
            
            
            
            UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseCategoryWithAnimaiton:)];
            _topView.userInteractionEnabled = YES;
            [_topView addGestureRecognizer:tap];
            
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_topView.mas_left).offset(10);
                make.centerY.equalTo(_topView);
                
            }];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(14);
                make.height.mas_equalTo(14);
                make.right.equalTo(_topView).offset(-20);
                make.centerY.equalTo(_topView);
            }];
            [rightL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(imageView.mas_left).offset(-8);
                make.centerY.equalTo(imageView.mas_centerY);
                make.left.lessThanOrEqualTo(label.mas_right).offset(4);
                
            }];
    
        }
        return _topView;


}
- (void)chooseCategoryWithAnimaiton:(BOOL)isAnimated{
    
    HELChooseCategoryVC *vc = [HELChooseCategoryVC new];
    weakSelf_define;
    
    vc.callBack = ^(NSString *name,NSNumber *cateId){
        UILabel *label = [weakSelf.topView viewWithTag:201];
        label.text = name;
        weakSelf.title = name;
        weakSelf.catogryId = [cateId integerValue];
        [weakSelf requestData:YES];
    };
  
    [self.navigationController pushViewController:vc animated:!isAnimated];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.topView.frame = CGRectMake(0, 0, kScreenWidth, 49);
    [self.view addSubview:self.topView];
    self.tableView.frame = CGRectMake(0, 49, kScreenWidth, kScreenHeight - 64- 49);
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELCourseCell" bundle:nil] forCellReuseIdentifier:@"HELCourseCell"];
    self.title = self.catogryName;
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf requestData:YES];
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
         [weakSelf requestData:NO];
    }];
    
    if (self.catogryId<0) {
        
        [self chooseCategoryWithAnimaiton:YES];
    }else{
      [self requestData:YES];
    }
    
   
}


- (void)requestData:(BOOL)isRecover{
    
    
    static NSUInteger page = 1;
    if (isRecover) {
        page =1;
    }else{
        page ++;
    }
   

    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(page);
    params[@"rows"] = @(15);
    if (self.catogryId>0) {
         params[@"classificationId"] = @(self.catogryId);
    }
   
    weakSelf_define;
    self.tableView.loading = YES;
    [[HELNetworkManager sharedMg] POST:helCourseSearchUrl parameters:params success:^(id responseObject) {
        
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage =[responseObject[@"data"][@"totalPage"] integerValue];
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELCourseListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
           
            [weakSelf.tableView reloadData];
            
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
    
    
    

    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELCourseCell"];
    
    cell.courseListModel = self.dataArray[indexPath.row];
    
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HELCourseListModel *model = self.dataArray[indexPath.row];
    NHCourseDetailVC *vc =[NHCourseDetailVC new];
    vc.courseId = [model.id integerValue];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}






@end
